﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BenefitHelper.View
{
    /// <summary>
    /// 操作员日分时交易数据
    /// </summary>
    public class OperatorDayTradeInfo
    {
        public List<OperatorDayTradeInfoData> Datas { get; set; }
        /// <summary>
        /// 根据关键字查询交易员当天分时情况
        /// </summary>
        /// <param name="pdate">查询日期</param>
        /// <param name="operatorid">操作员编号</param>
        /// <param name="key">profit？手续费？</param>
        public OperatorDayTradeInfo GetOperatorDayTradeInfo(int tradeHistoryId, int operatorid, string key, DB.DBManager db)
        {
            OperatorDayTradeInfo operatordayinfo = new OperatorDayTradeInfo();
            operatordayinfo.Datas = new List<OperatorDayTradeInfoData>();
            List<Sys.Account> accounts = new Sys.DayOperatorAccount().GetDayOperatorAccounts(new Sys.TradeHistory().GetDateTimeFromTradeHistoryId(tradeHistoryId, db), operatorid, db);
            foreach (Sys.Account account in accounts)
            {
                List<Data.Day.DayTradeInfo> infos = new Data.Day.DayTradeInfo().GetAccountDayTradeInfos(tradeHistoryId, account.Id, db);

                if (key.Equals("profit"))
                {
                    var allSum = infos.Sum(a => a.Profit);
                    if (allSum == 0)
                    {
                        allSum = 1;
                    }
                    foreach (Data.Day.DayTradeInfo info in infos)
                    {
                        OperatorDayTradeInfoData data = new OperatorDayTradeInfoData();
                        data.BillCount = info.BillCount;
                        data.SumValue = info.Profit;
                        data.Percent = Convert.ToInt32(data.SumValue / allSum) * 100;
                        switch (info.State)
                        {
                            case 0:
                                data.Clock = 1;
                                break;
                            case 1:
                                data.Clock = 2;
                                break;
                            case 2:
                                data.Clock = 3;
                                break;
                            case 3:
                                data.Clock = 4;
                                break;
                            case 4:
                                data.Clock = 5;
                                break;
                            case 5:
                                data.Clock = 6;
                                break;
                            case 6:
                                data.Clock = 7;
                                break;
                            case 7:
                                data.Clock = 8;
                                break;
                            case 8:
                                data.Clock = 9;
                                break;
                        }
                        bool isexist = false;
                        foreach (OperatorDayTradeInfoData d in operatordayinfo.Datas)
                        {
                            if (d.Clock == data.Clock)
                            {
                                isexist = true;
                                break;
                            }
                        }
                        if (!isexist)
                        {
                            operatordayinfo.Datas.Add(data);
                        }
                        else
                        {
                            OperatorDayTradeInfoData d = operatordayinfo.Datas.Where(a => a.Clock == data.Clock).First();
                            d.SumValue += data.SumValue;
                            d.BillCount += data.BillCount;
                        }

                    }
                }
                else if (key.Equals("billcount"))
                {
                    var allSum = infos.Sum(a => a.BillCount);
                    if (allSum == 0)
                    {
                        allSum = 1;
                    }
                    foreach (Data.Day.DayTradeInfo info in infos)
                    {
                        OperatorDayTradeInfoData data = new OperatorDayTradeInfoData();
                        data.BillCount = info.BillCount;
                        data.SumValue = info.BillCount;
                        data.Percent = Convert.ToInt32(data.SumValue / allSum) * 100;
                        switch (info.State)
                        {
                            case 0:
                                data.Clock = 1;
                                break;
                            case 1:
                                data.Clock = 2;
                                break;
                            case 2:
                                data.Clock = 3;
                                break;
                            case 3:
                                data.Clock = 4;
                                break;
                            case 4:
                                data.Clock = 5;
                                break;
                            case 5:
                                data.Clock = 6;
                                break;
                            case 6:
                                data.Clock = 7;
                                break;
                            case 7:
                                data.Clock = 8;
                                break;
                            case 8:
                                data.Clock = 9;
                                break;
                        }
                        bool isexist = false;
                        foreach (OperatorDayTradeInfoData d in operatordayinfo.Datas)
                        {
                            if (d.Clock == data.Clock)
                            {
                                isexist = true;
                                break;
                            }
                        }
                        if (!isexist)
                        {
                            operatordayinfo.Datas.Add(data);
                        }
                        else
                        {
                            OperatorDayTradeInfoData d = operatordayinfo.Datas.Where(a => a.Clock == data.Clock).First();
                            d.SumValue += data.SumValue;
                            d.BillCount += data.BillCount;
                        }
                    }
                }
                else if (key.Equals("sumlost"))
                {
                    var allSum = infos.Sum(a => a.SumLost);
                    if (allSum == 0)
                    {
                        allSum = 1;
                    }
                    foreach (Data.Day.DayTradeInfo info in infos)
                    {
                        OperatorDayTradeInfoData data = new OperatorDayTradeInfoData();
                        data.BillCount = info.BillCount;
                        data.SumValue = info.SumLost;
                        data.Percent = Convert.ToInt32(data.SumValue / allSum) * 100;
                        //    info.State.ToString().Debug();
                        switch (info.State)
                        {
                            case 0:
                                data.Clock = 1;
                                break;
                            case 1:
                                data.Clock = 2;
                                break;
                            case 2:
                                data.Clock = 3;
                                break;
                            case 3:
                                data.Clock = 4;
                                break;
                            case 4:
                                data.Clock = 5;
                                break;
                            case 5:
                                data.Clock = 6;
                                break;
                            case 6:
                                data.Clock = 7;
                                break;
                            case 7:
                                data.Clock = 8;
                                break;
                            case 8:
                                data.Clock = 9;
                                break;
                        }
                        bool isexist = false;
                        foreach (OperatorDayTradeInfoData d in operatordayinfo.Datas)
                        {
                            if (d.Clock == data.Clock)
                            {
                                isexist = true;
                                break;
                            }
                        }
                        if (!isexist)
                        {
                            operatordayinfo.Datas.Add(data);
                        }
                        else
                        {
                            OperatorDayTradeInfoData d = operatordayinfo.Datas.Where(a => a.Clock == data.Clock).First();
                            d.SumValue += data.SumValue;
                            d.BillCount += data.BillCount;
                        }
                    }
                }
                else if (key.Equals("sumwin"))
                {
                    var allSum = infos.Sum(a => a.SumWin);
                    if (allSum == 0)
                    {
                        allSum = 1;
                    }
                    foreach (Data.Day.DayTradeInfo info in infos)
                    {
                        OperatorDayTradeInfoData data = new OperatorDayTradeInfoData();
                        data.BillCount = info.BillCount;
                        data.SumValue = info.SumWin;
                        data.Percent = Convert.ToInt32(data.SumValue / allSum) * 100;
                        switch (info.State)
                        {
                            case 0:
                                data.Clock = 1;
                                break;
                            case 1:
                                data.Clock = 2;
                                break;
                            case 2:
                                data.Clock = 3;
                                break;
                            case 3:
                                data.Clock = 4;
                                break;
                            case 4:
                                data.Clock = 5;
                                break;
                            case 5:
                                data.Clock = 6;
                                break;
                            case 6:
                                data.Clock = 7;
                                break;
                            case 7:
                                data.Clock = 8;
                                break;
                            case 8:
                                data.Clock = 9;
                                break;
                        }
                        bool isexist = false;
                        foreach (OperatorDayTradeInfoData d in operatordayinfo.Datas)
                        {
                            if (d.Clock == data.Clock)
                            {
                                isexist = true;
                                break;
                            }
                        }
                        if (!isexist)
                        {
                            operatordayinfo.Datas.Add(data);
                        }
                        else
                        {
                            OperatorDayTradeInfoData d = operatordayinfo.Datas.Where(a => a.Clock == data.Clock).First();
                            d.SumValue += data.SumValue;
                            d.BillCount += data.BillCount;
                        }
                    }
                }

            }
            return operatordayinfo;
        }
    }

    public class OperatorDayTradeInfoData
    {
        public int Id { get; set; }

        public int OperatorId { get; set; }

        public virtual Sys.Operator Operator { get; set; }

        public int TradeHistoryId { get; set; }

        public virtual Sys.TradeHistory TradeHistory { get; set; }



        public int Clock { get; set; }

        public double BillCount { get; set; }

        public double SumValue { get; set; }

        public double Percent { get; set; }
    }
}
